package com.jinxin.platform.cdacsrecord.core.task;

import com.jinxin.platform.cdacsrecord.core.mapper.JurenAcsControlUserInfoMapper;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlUserInfoDO;
import com.jinxin.platform.cdacsrecord.core.utils.IdCardUtil;
import com.jinxin.platform.cdacsrecord.core.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;

/**
 * Description: bccah
 * Created by 34759 on 2019/8/16 15:57
 */
@EnableScheduling
@Configuration
@Component
@Slf4j
public class ScheduledTask {
    @Autowired
    private JurenAcsControlUserInfoMapper userInfoMapper;

    /**
     * 每月最后一天更新年龄
     */
    @Scheduled(cron = "0 59 23 28-31 * ?")
    public void updateUserAge(){
        final Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
            List<AcsControlUserInfoDO> userInfoList = userInfoMapper.selAll();
            userInfoList.forEach(item->{
                String userCard = item.getUserCard();
                if(IdCardUtil.isValidatedAllIdcard(userCard)){
                    AcsControlUserInfoDO userInfoDO = new AcsControlUserInfoDO();
                    userInfoDO.setUserId(item.getUserId());
                    userInfoDO.setAge(Utils.getPersonAgeFromIdCard(userCard));
                    userInfoMapper.updateByPrimaryKey(userInfoDO);
                }
            });
        }
    }

}
